package com.study.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisHash {

	public static void main(String[] args) {
		Jedis jedis = RedisUtil.getJedis();
		//将user的id赋值为1
		jedis.hset("user", "id", "1");
		//获取user的id值
		String id = jedis.hget("user", "id");
		System.out.println("id:" + id);
		
		//设置user的name为zs,age为18
		Map<String,String> hash = new HashMap<>();
		hash.put("name", "zs");
		hash.put("age", "18");
		jedis.hmset("user", hash);
		//获取user的id,name,age值
		List<String> hList = jedis.hmget("user", "id","name","age");
		System.out.print("user的id,name,age值为：");
		for (String value : hList) {
			System.out.print(value);
		}
		System.out.println();
		
		//查看user是否有id字段
		boolean hexists = jedis.hexists("user", "id");
		System.out.println("user是否有id字段:" + hexists);
		
		//删除user中的age字段
		jedis.hdel("user", "age");
		System.out.print("删除user中的age字段后user的字段：");
		//获取user的所有字段
		Set<String> keys = jedis.hkeys("user");
		for (String key : keys) {
			System.out.print(key);
		}
		System.out.println();
		//将user的name值改为zhangsan
		jedis.hset("user", "name", "zhangsan");
		//获取user字段的数量
		long length = jedis.hlen("user");
		System.out.println("user字段的数量：" + length);
		
		//获取user字段的所有值
		List<String> values = jedis.hvals("user");
		System.out.print("user字段所有值：");
		for (String value : values) {
			System.out.print(value);
		}
		System.out.println();
		
		//获取user的所有字段及对应的值
		Map<String,String> map = jedis.hgetAll("user");
		for (String key : map.keySet()) {
			System.out.println(key + "->" + map.get(key));
		}
		
		jedis.close();
	}
}
